import 'package:flutter/material.dart';


class ValUIKitBaseDecoration {
  static Decoration getShapeDecoration({
    Color? bg,
    double radius = 90,
    Color? borderColor,
    double borderWidth = 0.5,
    BorderStyle borderStyle = BorderStyle.solid,
    DecorationImage? image,
    Gradient? gradient,
    List<BoxShadow>? shadows,
  }) {
    return ShapeDecoration(
        color: bg,
        image: image,
        gradient: gradient,
        shadows: shadows,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(radius),
          side: borderColor == null || borderWidth == 0
              ? BorderSide.none
              : BorderSide(
            color: borderColor,
            width: borderWidth,
            style: borderStyle,
          ),
        ));
  }

  ///获取某个方向的划线,主要用于自定义某个描边
  static Decoration getBorder({
    Color? bg,
    BorderSide? left,
    BorderSide? top,
    BorderSide? right,
    BorderSide? bottom,
    DecorationImage? image,
    List<BoxShadow>? boxShadow,
    Gradient? gradient,
    BlendMode? backgroundBlendMode,
    BoxShape shape = BoxShape.rectangle,
  }) {
    return BoxDecoration(
      color: bg,
      image: image,
      boxShadow: boxShadow,
      gradient: gradient,
      backgroundBlendMode: backgroundBlendMode,
      shape: shape,
      border: Border(
        left: left ?? BorderSide.none,
        top: top ?? BorderSide.none,
        right: right ?? BorderSide.none,
        bottom: bottom ?? BorderSide.none,
      ),
    );
  }

  ///获取某个方向的圆角,主要用于自定义某个圆角
  static Decoration getRadius({
    Color? bg,
    double topLeft = 0,
    double topRight = 0,
    double bottomLeft = 0,
    double bottomRight = 0,
    double all = 0,
    DecorationImage? image,
    List<BoxShadow>? boxShadow,
    Gradient? gradient,
    BlendMode? backgroundBlendMode,
    BoxShape shape = BoxShape.rectangle,
  }) {
    var borderRadius = all > 0
        ? BorderRadius.all(Radius.circular(all))
        : BorderRadius.only(
      topLeft: Radius.circular(topLeft),
      topRight: Radius.circular(topRight),
      bottomLeft: Radius.circular(bottomLeft),
      bottomRight: Radius.circular(bottomRight),
    );
    return BoxDecoration(
      color: bg,
      image: image,
      boxShadow: boxShadow,
      gradient: gradient,
      backgroundBlendMode: backgroundBlendMode,
      shape: shape,
      borderRadius: borderRadius,
    );
  }

  ///获取一个阴影装饰
  static Decoration getShadow({
    Color bg = const Color(0xFFFFFFFF),
    Color borderColor = const Color(0x300aef3d),
    double borderWidth = 0.5,
    double radius = 90,
    Color shadowColor = const Color(0x200aef3d),
    Offset shadowOffset = Offset.zero,
    double shadowBlurRadius = 6,
    List<BoxShadow>? boxShadow,
  }) {
    boxShadow ??= [
      BoxShadow(
        color: shadowColor,
        offset: shadowOffset,
        blurRadius: shadowBlurRadius,
      )
    ];
    return BoxDecoration(
      color: bg,
      border: Border.fromBorderSide(BorderSide(
        color: borderColor,
        width: borderWidth,
        style: BorderStyle.solid,
      )),
      borderRadius: BorderRadius.all(Radius.circular(radius)),
      boxShadow: boxShadow,
    );
  }
}

class CommonTabIndicator extends Decoration {
  const CommonTabIndicator({
    this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
    this.insets = EdgeInsets.zero,
  });

  /// The color and weight of the horizontal line drawn below the selected tab.
  final BorderSide borderSide;

  /// Locates the selected tab's underline relative to the tab's boundary.
  ///
  /// The [TabBar.indicatorSize] property can be used to define the tab
  /// indicator's bounds in terms of its (centered) tab widget with
  /// [TabBarIndicatorSize.label], or the entire tab with
  /// [TabBarIndicatorSize.tab].
  final EdgeInsetsGeometry insets;

  @override
  Decoration? lerpFrom(Decoration? a, double t) {
    if (a is CommonTabIndicator) {
      return CommonTabIndicator(
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
        insets: EdgeInsetsGeometry.lerp(a.insets, insets, t)!,
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
  Decoration? lerpTo(Decoration? b, double t) {
    if (b is CommonTabIndicator) {
      return CommonTabIndicator(
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
        insets: EdgeInsetsGeometry.lerp(insets, b.insets, t)!,
      );
    }
    return super.lerpTo(b, t);
  }

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _CommonTabIndicatorPainter(this, onChanged);
  }

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
    return Rect.fromLTWH(
      indicator.left,
      indicator.bottom - borderSide.width,
      indicator.width,
      borderSide.width,
    );
  }

  @override
  Path getClipPath(Rect rect, TextDirection textDirection) {
    return Path()..addRect(_indicatorRectFor(rect, textDirection));
  }
}

class _CommonTabIndicatorPainter extends BoxPainter {
  _CommonTabIndicatorPainter(this.decoration, VoidCallback? onChanged) : super(onChanged);

  final CommonTabIndicator decoration;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration.size != null);
    final Rect rect = offset & configuration.size!;
    final TextDirection textDirection = configuration.textDirection!;
    final Rect indicator = decoration._indicatorRectFor(rect, textDirection).deflate(decoration.borderSide.width / 2.0);
    final Paint paint = decoration.borderSide.toPaint()..strokeCap = StrokeCap.round;
    canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  }
}
